﻿using MediatR;
using MS.BasicData.Domain;
using MS.BasicData.Infrastructure;
using MS.BasicData.Infrastructure.IBaseRepository;
using System.ComponentModel.DataAnnotations;

namespace MS.BasicData.Read.API.Applications.UserInfos
{
    public class ListQueryRequestHandler : IRequestHandler<ListQueryRequestModel, ApiRestult>
    {
        /// <summary>
        /// 用户信息仓储
        /// </summary>
        private readonly IUserBaseRepository _userbaserepository;
        /// <summary>
        /// 角色仓储
        /// </summary>
        private readonly IRoleBaseRepository _roleRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userbaserepository"></param>
        /// <param name="roleRepository"></param>
        public ListQueryRequestHandler(IUserBaseRepository userbaserepository, IRoleBaseRepository roleRepository)
        {
            _userbaserepository = userbaserepository;
            _roleRepository = roleRepository;
        }

        public async Task<ApiRestult> Handle(ListQueryRequestModel request, CancellationToken cancellationToken)
        {

            ApiRestult restult = new ApiRestult()
            {
                isSuc = true,
                Message = "获取用户信息列表成功",
                Code = Restult.Success
            };
            var list = await _userbaserepository.GetAllAsync(a => a.IsDeleted == false);
            if (!string.IsNullOrEmpty(request.UserName))
            {
                list = list.Where(x => x.UserName.Contains(request.UserName)).ToList();
            }
            var role = await _roleRepository.GetAllAsync();

            var query = from a in list
                        select new ListQueryResponseModel()
                        {
                            UserInfoId = a.UserInfoId,
                            UserName = a.UserName,
                            UserNick = a.UserNick,
                            Password = a.Password,
                            ErrNum = a.ErrNum,
                            IsSuc = a.IsSuc
                        };
                restult.Data = query.ToList();
                return restult;
        }
    }

    public class ListQueryRequestModel : IRequest<ApiRestult>
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string? UserName { get; set; }
    }
    public class ListQueryResponseModel
    {
        /// <summary>
        /// 用户主键
        /// </summary>
        public int UserInfoId { get; set; }
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; } = string.Empty;
        /// <summary>
        /// 角色ID
        /// </summary>
        public int RoleId { get; set; }
        /// <summary>
        /// 角色名称
        /// </summary>
        public string RoleName { get; set; } = string.Empty;

        /// <summary>
        /// 角色描述
        /// </summary>
        public string Description { get; set; } = string.Empty;
        /// <summary>
        /// 用户昵称
        /// </summary>
        public string UserNick { get; set; } = string.Empty;
        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; } = string.Empty;
        /// <summary>
        /// 错误次数
        /// </summary>
        public int ErrNum { get; set; }
        /// <summary>
        /// 用户状态
        /// </summary>
        public bool IsSuc { get; set; }
    }


}
